<meta charset="utf-8">
<?php
include_once("database.php");

$page = $_GET["p"];

//表格行数
$pagesize = 5;

//展示页码
$showpage = 5;

$startnums = ($page - 1) * $pagesize;
$currentpagenum = $pagesize;
$currentsql = "select * from page limit $startnums,$currentpagenum";
$result = mysql_query($currentsql);

//释放结果
mysql_free_result($result);

//获取数据总数
$total_sql = "select count(*) from page";
$total_result = mysql_fetch_array(mysql_query($total_sql));
$total = $total_result[0];
// echo "总条数：".$total;exit;

//计算页数
$total_pages = ceil($total / $pagesize);

//关闭链接
mysql_close($connection);


/*3 显示数据 + 分页条*/
$page_banner = "<div class='page'>";

//计算偏移量
$pageoffset = ($showpage - 1) / 2;

if($page > 1) {
	$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
	$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page - 1)."'>&lt;上一页</a>";
} else {
	$page_banner .= "<span class='disable'>首页</a></span>";
	$page_banner .= "<span class='disable'>&lt;上一页</a></span>";
}

//初始化数据
$start = 1;//开始页码
$end = $total_pages;//结束页码

if($total_pages > $showpage) {
	if($page > $pageoffset + 1) {
		$page_banner .= "...";
	}	

	if($page > $pageoffset) {
		$start = $page - $pageoffset;
		$end = $total_pages > $page + $pageoffset ? $page + $pageoffset : $total_pages;
	} else {
		$start = 1;
		$end = $total_pages > $showpage ? $showpage : $total_pages;
	}

	if($page + $pageoffset > $total_pages) {
		$start = $start - ($page + $pageoffset - $end);
	}

}

for($i = $start; $i <= $end; $i++) {
	if($page == $i) {
		$page_banner .= "<span class='current'>{$i}</span>";
	} else {
		$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".$i."'>{$i}</a>";
	}
}

//尾部省略
if($total_pages > $showpage && $total_pages > $page + $pageoffset) {
	$page_banner .= "...";
}
if($page < $total_pages) {
	$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page + 1)."'>下一页&gt;</a>";
	$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($total_pages)."'>尾页</a>";
} else {
	$page_banner .= "<span class='disable'>下一页&gt;</a></span>";
	$page_banner .= "<span class='disable'>尾页</a></span>";
}

$page_banner .= "共{$total_pages}页,";
$page_banner .= "<form action='fenye.php' method='get'>";
$page_banner .= "到第<input type='text' size='2' name='p'>页";
$page_banner .= "<input type='submit' value='确定'>";
$page_banner .= "</form></div>";

echo $page_banner;
?>